package org.dreamfly.core.page;

import java.io.Serializable;

public class PageInfo implements Serializable {
	private Long totalRow = 0L; // 总共记录数
	private Long totalPage = 0L; // 总共页数
	private Long currentPage = 1L; // 当前页，默认为1
	private Long clientPage = 1L; //客户端页码
	private Long maxQueryTimes; //最大的查询service次数
	private Long queryTime = 1L; //查询service次数 与原来的page相同
	private Long pageSize = 3L; // 页的大小
	private Long begin ;	//页面上取list时候的开始位置
	private Long end ;	//页面上取list时候的结束位置
	private Long totalClientPage;
	private Long havePage;
	private boolean hasPrevious = false;
	private boolean hasNext = false;
	private boolean bof = false;
	private boolean eof = false;
	private Long beginPage = 0L; // 数字页码的开始数
	private Long endPage = 0L; // 数字页码的结束数

	/**
	 * 构造方法
	 * 
	 * @param totalRow 总记录数
	 * 
	 * @param pageSize 页的大小
	 * 
	 * @param queryTime 数据库用的页码
	 */
	public PageInfo(Long totalRow, Long pageSize, Long queryTime) {
		this.totalRow = totalRow;
		this.pageSize = pageSize;
		this.queryTime = queryTime;
		
		// 根据页大小和总记录数计算出总页数
		this.totalPage = countTotalPage(this.pageSize, this.totalRow);

		// 修正当前页
		setCurrentPage(queryTime);

		init();
		
	}
	
	public PageInfo(Long totalRow, Long pageSize) {
		super();
		this.totalRow = totalRow;
		this.pageSize = pageSize;
		// 根据页大小和总记录数计算出总页数
		this.totalPage = countTotalPage(this.pageSize, this.totalRow);
		init();
		if(totalPage<=5){
			beginPage = 1L;
			endPage = totalPage;
		}else{
			if(clientPage<=3){
				beginPage = 1L;
				endPage = 5L;
			}
			else{
				beginPage = clientPage-2L;
				if(clientPage+2L>totalPage){
					endPage=totalPage;
				}
				else{
					endPage = clientPage+2L;
				}
			}
		}
	}

	public Long getTotalPage() {
		return totalPage;
	}

	public Long getCurrentPage() {
		return this.currentPage;
	}

	// 修正计算当前页
	public void setCurrentPage(Long currentPage) {
		if (currentPage > this.totalPage) {
			this.currentPage = this.totalPage;
		} else if (currentPage < 1L) {
			this.currentPage = 1L;
		} else {
			this.currentPage = currentPage;
		}

	}

	// 获取分页大小
	public Long getPageSize() {
		return pageSize;
	}

	// 设置分页大小
	public void setPageSize(Long pageSize) {
		this.pageSize = pageSize;
	}

	// 获取当前页记录的开始索引
	public Long getBeginIndex() {
		Long beginIndex = (currentPage - 1L) * pageSize; // 索引下标从0开始
		return beginIndex;
	}

	// 计算总页数
	public Long countTotalPage(Long pageSize, Long totalRow) {
		Long totalPage = totalRow % pageSize == 0 ? totalRow / pageSize
				: totalRow / pageSize + 1L;
		return totalPage;
	}

	// 返回下一页的页码
	public Long getNextPage() {
		if (currentPage + 1L >= this.totalPage) { // 如果当前页已经是最后页 则返回最大页
			return this.totalPage;
		}
		return currentPage + 1L;
	}

	// 返回前一页的页码
	public Long getPreviousPage() {
		if (currentPage - 1L <= 1L) {
			return 1L;
		} else {
			return currentPage - 1L;
		}
	}

	public boolean isHasPrevious() {
		return hasPrevious;
	}

	public boolean isHasNext() {
		return hasNext;
	}

	public boolean isBof() {
		return bof;
	}

	public boolean isEof() {
		return eof;
	}

	public boolean hasNext() {
		return currentPage < this.totalPage;
	}

	public boolean hasPrevious() {
		return currentPage > 1L;
	}

	public boolean isFirst() {
		return currentPage == 1L;
	}

	public boolean isLast() {
		return currentPage >= this.totalPage;
	}
	
	public Long getBeginPage() {
		if(totalPage<=5){
			beginPage = 1L;
			endPage = totalPage;
		}else{
			if(clientPage<=3){
				beginPage = 1L;
				endPage = 5L;
			}
			else{
				beginPage = clientPage-2L;
				if(clientPage+2L>totalPage){
					endPage=totalPage;
				}
				else{
					endPage = clientPage+2L;
				}
			}
		}
		return beginPage;
	}

	public void setBeginPage(Long beginPage) {
		this.beginPage = beginPage;
	}

	public Long getEndPage() {
		return endPage;
	}

	public void setEndPage(Long endPage) {
		this.endPage = endPage;
	}
	
	
	
	public Long getTotalRow() {
		return totalRow;
	}

	public void setTotalRow(Long totalRow) {
		this.totalRow = totalRow;
	}
	
	

	public Long getClientPage() {
		return clientPage;
	}

	public void setClientPage(Long clientPage) {
		this.clientPage = clientPage;
	}
	
	public Long getTotalClientPage() {
		return totalClientPage;
	}

	public void setTotalClientPage(Long totalClientPage) {
		this.totalClientPage = totalClientPage;
	}

	public Long getMaxQueryTimes() {
		return maxQueryTimes;
	}

	public void setMaxQueryTimes(Long maxQueryTimes) {
		this.maxQueryTimes = maxQueryTimes;
	}
	

	public Long getQueryTime() {
		return queryTime;
	}

	public void setQueryTime(Long queryTime) {
		this.queryTime = queryTime;
	}
	
	

	public Long getBegin() {
		begin = (clientPage-1L)*pageSize;
		return begin;
	}

	public Long getEnd() {
		end = (clientPage)*pageSize-1L;
		return end;
	}

	// 初始化信息
	private void init() {
		this.hasNext = hasNext();
		this.hasPrevious = hasPrevious();
		this.bof = isFirst();
		this.eof = isLast();
	}
	
}
